package com.leo;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.PasswordField;
import javafx.scene.control.Tooltip;
import javafx.scene.text.Font;
import javafx.stage.Stage;

/**
 * @ClassName PasswordFieldDemo
 * @Description 密码框
 * @Author wangss
 * @date 2020.05.17 00:25
 * @Version 1.0
 */
public class PasswordFieldDemo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        final PasswordField textField = new PasswordField();

        textField.setLayoutX(10d);
        textField.setLayoutY(80d);

        textField.setPrefWidth(200d);

        textField.setFont(Font.font(24));

        // 提示
        Tooltip tooltip = new Tooltip("提示");
        tooltip.setFont(Font.font(20));
        textField.setTooltip(tooltip);

        textField.setPromptText("请输入内容");
        textField.setFocusTraversable(false);

        // 监听文本框的变化
        textField.textProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                System.out.println("打印输入内容：" + newValue);
                if (newValue.length() > 7) {
                    textField.setText(oldValue);
                }
            }
        });

        // 监听选择的文本
        textField.selectedTextProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                System.out.println("选择的文本内容：" + newValue);
            }
        });

        Group group = new Group();
        group.getChildren().add(textField);

        Scene scene = new Scene(group);

        primaryStage.setScene(scene);
        primaryStage.setTitle("input box");
        primaryStage.setWidth(600d);
        primaryStage.setHeight(600d);
        primaryStage.show();
    }
}
